<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ include file="/checkLogin.jsp" %>
<%@ page import="java.util.*,org.activiti.engine.history.*,com.netqin.kingviker.ProcessCustomService,org.activiti.engine.task.*,org.activiti.engine.repository.*,org.activiti.engine.runtime.*,org.apache.commons.lang.*"%>
<%
		List<ProcessDefinition> pdList =ProcessCustomService.getRepositoryService().createProcessDefinitionQuery().list();
		List<ProcessInstance> piList = ProcessCustomService.getRuntimeService().createProcessInstanceQuery().list();
		List<Task> taskList = ProcessCustomService.getTaskService().createTaskQuery().list();
		List<Task> userTasks = null;
		String username = (String)session.getAttribute("username");
		if(StringUtils.isNotBlank(username)){ 
			userTasks = ProcessCustomService.getTaskService().createTaskQuery().taskAssignee(username).list();
			}
		List<HistoricProcessInstance> hpiList = ProcessCustomService.getHistoryService().createHistoricProcessInstanceQuery().finished().list();
		
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>index</title>
  </head>
  <body> 
    <a href="deploy.jsp">发布新流程</a>&nbsp;[username: <%=username %>]<a href="login.jsp">登陆</a>

    <table border="1" width="100%">
      <h3>流程定义</h3>
      <thead>
        <tr>
          <td>id</td>
          <td>name</td>
          <td>version</td>
          <td>&nbsp;</td>
        </tr>
      </thead>
      <tbody>
<%
	for (ProcessDefinition pd : pdList) {
%>
	    <tr>
	      <td><%=pd.getId() %></td>
	      <td><%=pd.getName() %></td>
	      <td><%=pd.getVersion() %></td>
	      <td>
	        <a href="remove.jsp?id=<%=pd.getDeploymentId() %>">remove</a>
	        &nbsp;|&nbsp;
	        <a href="start.jsp?id=<%=pd.getId() %>">start</a>
	      </td>
	    </tr>
<%
	}
%>
	  </tbody>
	</table> 

    <table border="1" width="100%">
      <h3>流程实例</h3>
      <thead>
        <tr>
          <td>id</td>
          <td>当前任务</td>
          <td>任务审批人</td>
          <td>&nbsp;</td>
        </tr>
      </thead>
      <tbody>
<%
	for (Task task : taskList) {
%>
	    <tr>
	      <td><%=task.getProcessInstanceId() %></td>
	      <td><%=task.getName() %></td>
	      <td><%=task.getAssignee() %></td>
	      <td><a href="view.jsp?taskId=<%=task.getId() %>">view</a></td>
	    </tr>
<%
	}
%>
	  </tbody>
	</table> 

    <table border="1" width="100%">
      <h3>待办任务</h3>
      <thead>
        <tr>
          <td>id</td>
          <td>name</td>
          <td>&nbsp;</td>
        </tr>
      </thead>
      <tbody>
<%
	if(userTasks!=null){
		for(Task task : userTasks) {
%>
	    <tr>
	      <td><%=task.getId() %></td>
	      <td><%=task.getName() %></td>
	      <td><a href="form.jsp?id=<%=task.getId() %>">view</a></td>
	    </tr>
<%
	}}
%>
	  </tbody>
	</table> 
    <table border="1" width="100%">
      <h3>历史流程</h3>
      <thead>
        <tr>
          <td>id</td>
          <td>发起人</td>
          <td>开始时间</td>
          <td>结束时间</td>
        </tr>
      </thead>
      <tbody>
<%
		for(HistoricProcessInstance hpi : hpiList) {
%>
	    <tr>
	      <td><%=hpi.getId() %></td>
	      <td><%=ProcessCustomService.getHistoryService().createHistoricTaskInstanceQuery().processFinished().processInstanceId(hpi.getId()).orderByTaskId().asc().list().get(0).getAssignee()%></td>
	      <td><%=hpi.getStartTime() %></td>
	      <td><%=hpi.getEndTime() %></td>
	    </tr>
<%
	}
%>
	  </tbody>
	</table> 
  </body>
</html>
